package com.itqf.controller;

import com.itqf.entity.Address;
import com.itqf.entity.User;
import com.itqf.service.AddressService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/address")
public class AddressController extends BaseServlet{

    public  String show(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            session.setAttribute("msg", "需要先登录！");
            return Constants.FOW + "/login.jsp";
        }
        int userno = user.getUserno();
        AddressService addressService = new AddressServiceImpl();
        List<Address> addresses = addressService.findAllAddressByUserno(userno);
        req.setAttribute("list", addresses);
        return Constants.FOW + "/self_info.jsp";
    }
    public String add(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, SQLException {
        int userno = Integer.parseInt(req.getParameter("userno"));
        String aname = req.getParameter("aname");
        String phonenumber = req.getParameter("phonenumber");
        String adetail = req.getParameter("adetail");
        Address address = new Address();
        address.setUserNo(userno);
        address.setAname(aname);
        address.setPhonenumber(phonenumber);
        address.setAdetail(adetail);
        address.setAstate(0);
        System.out.println(address.toString());
        AddressService addressService = new AddressServiceImpl();
        addressService.saveAddress(address);

        return Constants.FOW + "/address?method=show";
    }
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String aid = req.getParameter("aid");
        AddressService addressService = new AddressServiceImpl();
        addressService.deleteAddressByAid(aid);

        return Constants.FOW + "/address?method=show";
    }
    public String setDefault(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String aid = req.getParameter("aid");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            session.setAttribute("msg", "需要先登录！");
            return Constants.FOW + "/login.jsp";
        }
        AddressService addressService = new AddressServiceImpl();
        addressService.astAddressDefaultByAid(aid,user.getUserno());

        return Constants.FOW + "/address?method=show";
    }
    public String update(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        int userno = Integer.parseInt(req.getParameter("userno"));
        String aname = req.getParameter("aname");
        String phonenumber = req.getParameter("phonenumber");
        String adetail = req.getParameter("adetail");
        int astate = Integer.parseInt(req.getParameter("astate"));
        Address address = new Address();
        address.setUserNo(userno);
        address.setAname(aname);
        address.setPhonenumber(phonenumber);
        address.setAdetail(adetail);
        address.setAstate(astate);
        AddressService addressService = new AddressServiceImpl();
        addressService.updateAddress(address);


        return Constants.FOW + "/address?method=show";
    }
}
